<?php

namespace app\stores\controller\miniapp\promotion;

use app\BaseController;
use app\common\model\DataConfigModel;
use app\stores\model\promotion\PromotionmoneydetailModel;
use app\stores\model\promotion\PromotionrecordModel;
use app\stores\model\withdraw\MemberWithdrawModel;

class Member extends BaseController{

   public function info(){
       $type = $this->request->type;
       $team_id = $this->request->teamId;
       $member_id = $this->request->memberId;
       $record = PromotionrecordModel::info(['member_id'=>$member_id,'team_id'=>$team_id]);
       $member['money'] = sprintf("%.2f",empty($record)?0:$record['money']);
       //用户提现手续费
       $dataconfig=new DataConfigModel();
       $info=$dataconfig->where(['id'=>1])->find();
       $member['user_money_ratio'] = $info['user_money_ratio'];
       //业绩
//       $p_w[] = ['team_id','=',$team_id];
//       if($type==1){
//           $p_w[] = ['member_id','=',$member_id];
//       }else{
//           $p_w[] = ['member_id','>',0];
//       }
//       $return_statis = PromotionmoneydetailModel::data_statis([],$p_w,$$team_id);
//       $member['total_performance'] = $return_statis['total_performance'];
//       //待解冻
//       $tj_where[] = ['team_id','=',$team_id];
//       if($type==1){
//           $tj_where[] = ['member_id','=',$member_id];
//       }else{
//           $tj_where[] = ['member_id','>',0];
//       }
//       $return_statis = PromotionmoneydetailModel::data_statis([],$tj_where,$$team_id);
//       $member['total_ing_money'] = $return_statis['total_ing_money'];
//       //已提现（到账）
//       $tx_where[] = ['type','=',1];
//       $tx_where[] = ['status','=',1];
//       $tx_where[] = ['team_id','=',$team_id];
//       $tx_where[] = ['is_delete','=',0];
//       if($type==1){
//           $tx_where[] = ['member_id','=',$member_id];
//       }else{
//           $tx_where[] = ['member_id','>',0];
//       }
//       $member['total_withdraw_money'] = MemberWithdrawModel::where($tx_where)->sum('money');

       return $this->success($member);
   }
   /*账户明细*/
   public function moneyDetail(){
       $type = $this->request->type;
       $team_id = $this->request->teamId;
       $date = $this->request->param('date',date('Y-m'));
       $money_type= $this->request->param('money_type/d',0);//0全部1收入2支出(提现失败得的不统计)3待解冻
       if(empty($date)){
           $date = date('Y-m');
       }
       $start_time = $date.'-01'.' 00:00:00';
       $end_time = date('Y-m-t',strtotime($start_time)).' 23:59:59';
       $member_id = $type==2?0:$this->request->memberId;
       $where['team_id'] = $team_id;
       $where['member_id'] = $member_id;
       $where['add_start_time'] = $start_time;
       $where['add_end_time'] = $end_time;
       //除开提现失败退回
       $withdraw_ids = MemberWithdrawModel::where(['team_id'=>$team_id,'member_id'=>$member_id,'is_delete'=>0,'status'=>-1])->column('withdraw_id');
       $where['withdraw_ids'] = $withdraw_ids;
       if($money_type==1) {
           $where['type'] = 1;
           $where['status'] = 1;
           //除开提现失败退回
           $withdraw_ids = MemberWithdrawModel::where(['team_id'=>$team_id,'member_id'=>$member_id,'is_delete'=>0,'status'=>-1])->column('withdraw_id');
           $where['withdraw_ids'] = $withdraw_ids;
       }else if($money_type==2){
           $where['type'] = -1;
           //除开提现失败退回
           $withdraw_ids = MemberWithdrawModel::where(['team_id'=>$team_id,'member_id'=>$member_id,'is_delete'=>0,'status'=>-1])->column('withdraw_id');
           $where['withdraw_ids'] = $withdraw_ids;
       }else if($money_type==3){
           $where['type'] = 1;
           $where['status'] = 0;
           $where['is_ing'] = 1;
       }

       $order = ['add_time'=>'desc'];

       $return = PromotionmoneydetailModel::list($where,$order,$this->request->page,$this->request->limit);

       $record = PromotionrecordModel::info(['member_id'=>$member_id,'team_id'=>$team_id]);

       $return['member_money'] = empty($record)?0:$record['money'];

       $tj_where[] = ['team_id','=',$team_id];
       $tj_where[] = ['member_id','=',$member_id];
       $tj_where[]=['add_time','>=',$start_time];
       $tj_where[]=['add_time','<=',$end_time];
       $return_statis = PromotionmoneydetailModel::data_statis([],$tj_where,$team_id);
       //支出
       $return['total_out_money'] = sprintf("%.2f",$return_statis['total_out_money']);
       //收入
       $return['total_in_money'] = sprintf("%.2f",$return_statis['total_in_money']);
       //待解冻
       $return['total_ing_money'] = sprintf("%.2f",$return_statis['total_ing_money']);

       return $this->success($return);
   }


}

